<?php
class site_mode {
	public $mode_type = "edit";
	public $mode_standard_url = ""; // index.php?page=allgemein
	public $mode_activation_url = ""; // &action=edit -> mode_get_var_name + mode_type
	
	// form
	public $mode_form_method = "post"; // alt. GET
	public $mode_submit_name = ""; // save
	public $mode_submit_value = ""; // bearbeiten
	public $mode_submit_img = ""; // ../img/note_ok.png
	
	// mode activation icon
	public $mode_get_var_name = "action"; // form mode_activation_url
	public $mode_btn_icon = ""; // ../img/note_edit.png
	
	public $column_structur = array();
	public $datarow = array();
	public $indexer = "id";
	
	
	public function __construct($type, $method, $standard_url, $action_get_var, $submit_name, $submit_value, $submit_icon_url, $mode_icon, Array $columns_key_value, $row_indexer) {
		// general settings
		if($type == "edit" || $type == "new" || $type == "show") {
			$this->mode_type = $type;
		}
		
		$this->mode_standard_url = $standard_url;
		
		// form
		if($method == "post" || $method == "get") {
			$this->mode_form_method = $method;
		}
		$this->mode_submit_name = $submit_name;
		$this->mode_submit_value = $submit_value;
		$this->mode_submit_img = $submit_icon_url;
		
		// mode activation icon
		$this->mode_get_var_name = $action_get_var;
		$this->mode_btn_icon = $mode_icon;
		
		$this->column_structur = $columns_key_value;
		$this->indexer = $row_indexer;
		
		$this->set_url();
	}
	
	public function set_datarow($row) {
		$this->datarow = $row;
	}
	
	/*
	 * Bearbeitet die url, die benutz wird, um den Modus zu aktivieren.
	 */
	private function set_url() {
		if(preg_match("/(\?)/", $this->mode_standard_url)) {
			$this->mode_activation_url = $this->mode_standard_url."&".$this->mode_get_var_name."=".$this->mode_type;
		} else {
			$this->mode_activation_url = $this->mode_standard_url."?".$this->mode_get_var_name."=".$this->mode_type;
		}
	}
	
	public function get_edit_row(Array $datarow) {
		$mode = true;
		$row = "";
		if($mode != false) {
			if(isset($_GET[$this->mode_get_var_name])) {
				if($_GET[$this->mode_get_var_name] == $this->mode_type) {
					$row .= "<form action=\"".$this->mode_standard_url."\" method=\"".$this->mode_form_method."\"><tr ".$this->tr_class.">";
					foreach($datarow as $key => $value) {
						$row .= "<td valign=\"middle\"><input type=\"text\" name=\"".$key."\" value=\"".$value."\" /></td>";
					}
					$row .= "<td><input type=\"hidden\" name=\"".$this->mode_submit_name."\" value=\"".$this->mode_submit_value."\" /><input type=\"image\" name=\"send\" src=\"".general::get_url(2, $this->mode_submit_img)."\" width=\"16px\" height=\"16px\" /></td></tr>";
					$row .= "</form>";
				}
			}
		}
		return $row;
	}
	
	public function get_new_row(Array $datarow) {
		$mode = true;
		$row = "";
		if($mode != false) {
			if(isset($_GET[$this->mode_get_var_name])) {
				if($_GET[$this->mode_get_var_name] == $this->mode_type) {
					$row .= "<form action=\"".$this->mode_standard_url."\" method=\"".$this->mode_form_method."\"><tr ".$this->tr_class.">";
					foreach($datarow as $key => $value) {
						$row .= "<td valign=\"middle\"><input type=\"text\" name=\"".$key."\" value=\"".$value."\" /></td>";
					}
					$row .= "<td><input type=\"hidden\" name=\"".$this->mode_submit_name."\" value=\"".$this->mode_submit_value."\" /><input type=\"image\" name=\"send\" src=\"".general::get_url(2, $this->mode_submit_img)."\" width=\"16px\" height=\"16px\" /></td></tr>";
					$row .= "</form>";
				}
			}
		}
		return $row;
	}
}
?>